//音视频设备
var videoArr=[];
var audioArr=[];
var audiooutArr=[];
var audio_stream,video_stream,audioout_stream;
var localstream;
//type:0-摄像头 1-音频


function getDevice(){
	console.log('getDevice');
	navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(err=>{console.log(err);});
}

function gotDevices(devices){
	var html='';
	var videoinput='';
	var audioinput='';
	var audiooutput='';
	for(var i=0;i<devices.length;i++){
		var deviceId= devices[i].deviceId;
		var kind= devices[i].kind;
		var label= devices[i].label;
		var groupId= devices[i].groupId;
		html+='<tr class="line">';
		html+='<td>'+devices[i].kind+"</td>";
		html+='<td>'+devices[i].label+"</td>";
		html+='<td>'+devices[i].deviceId+"</td>";
		html+='<td>'+devices[i].groupId+"</td>";
		html+='</tr>';
		console.log(devices[i]);
		if(kind=='audioinput'){
			audioArr.push(deviceId);
			audioinput+='<option value="'+deviceId+'">'+deviceId+'</option>';
		}
		if(kind=='videoinput'){
			videoArr.push(deviceId);
			videoinput+='<option value="'+deviceId+'">'+deviceId+'</option>';
		}
		if(kind=='audiooutput'){
			audiooutArr.push(deviceId);
			audiooutput+='<option value="'+deviceId+'">'+deviceId+'</option>';
		}
	}
	$('#devices').html(html);
	$('#video').html(videoinput);
	$('#audio').html(audioinput);
	$('#audioout').html(audiooutput);
	
}

//type:0-摄像头 1-音频 2-音频输出
function getMedia(type){
	var id='';
	var media = {};
	if(type==0)
	{
		id = $('#video').val();
		console.log(id);
		if(id==''||id==undefined)
		{
			media={video:true,audio:false};
		}
		else{
			media = {video:{videoId:id}};
		}
		navigator.getUserMedia(media,stream=>{
			video_stream = stream;
			console.log(stream);
			video_box.srcObject = stream;
		},e=>{console.error(e);});
	}
	
	if(type==1)//音频
	{
		id = $('#audio').val();
		if(id==''||id==undefined)
		{
			media={video:false,audio:true};
		}
		else
		{
			media = {audio:{videoId:id}};
		}
		navigator.getUserMedia(media,stream=>{
			audio_stream = stream;
			audio_box.srcObject = stream;
		},e=>{console.error(e);});
	}
		
	if(type==2)
	{
		id = $('#audioout').val();
		if(id==''||id==undefined)
		{
			media={video:false,audio:true};
		}
		else
		{
			media = {audio:{videoId:id}};
		}
		navigator.getUserMedia(media,stream=>{
			audioout_stream = stream;
			audioout_box.srcObject = stream;
		},e=>{console.error(e);});
	}
	if(type==3)
	{
		navigator.getUserMedia({video:true,audio:true},stream=>{
			video_box.srcObject = stream;
			localStream = stream;
			});
	}
}

//getMedia(3);


var mimeTypes = ['video/webm','audio/mpeg','audio/basic',"audio/x-wav"];

for(var i=0;i<mimeTypes.length;i++){
	if (!MediaRecorder.isTypeSupported(mimeTypes[i])) {
		console.log(mimeTypes[i] + ' is not Supported');
	}
}

var mediaRecorder;
var record_option={mimeType: 'video/webm;codecs=vp9'};
var recordBlobs=[];

function record(){
  if (!MediaRecorder.isTypeSupported(record_option.mimeType)) {
	console.log(record_option.mimeType + ' is not Supported');
	record_option = {mimeType: 'video/webm;codecs=vp8'};
	if (!MediaRecorder.isTypeSupported(record_option.mimeType)) {
	  console.log(record_option.mimeType + ' is not Supported');
	  record_option = {mimeType: 'video/webm'};
	  if (!MediaRecorder.isTypeSupported(record_option.mimeType)) {
		console.log(record_option.mimeType + ' is not Supported');
		record_option = {mimeType: ''};
	  }
	}
  }
  console.log("record_option.mimeType:"+record_option.mimeType);
  recordBlobs=[];
  
  try {
		mediaRecorder = new MediaRecorder(localstream, record_option);
	  } catch (e) {
		console.error('Exception while creating MediaRecorder: ' + e);
		alert('Exception while creating MediaRecorder: '
		  + e + '. mimeType: ' + options.mimeType);
		return;
	  }
	mediaRecorder.onstop = handleStop;
	mediaRecorder.ondataavailable = handleDataAvailable;
	mediaRecorder.start(); // collect 10ms of data
	console.log('MediaRecorder started', mediaRecorder);  
}



function handleStop(event){
	console.log('stop',event);
}

function handleDataAvailable(event){
	console.log('avalable');
	if(event.data&&event.data.size>0){
		recordBlobs.push(event.data);
	}
}

function stopRecord(){
	mediaRecorder.stop();
	upload();
}

function play(){
	var superBuffer = new Blob(recordBlobs, {type: 'video/webm'});
	remote.src = window.URL.createObjectURL(superBuffer);
}


function down(){
	var blob = new Blob(recordBlobs, {type: 'video/webm'});
	var url = window.URL.createObjectURL(blob);
	var a = document.createElement('a');
	console.log(blob);
	a.style.display = 'none';
	a.href = url;
	a.download = 'test.webm';
	document.body.appendChild(a);
	a.click();
	setTimeout(function() {
		document.body.removeChild(a);
		window.URL.revokeObjectURL(url);
	}, 100);
}

function upload(){
	var blob = new Blob(recordBlobs, {type: 'video/webm'});
	var formData = new FormData();
	formData.append("video",blob,"camera.webm");
	$.ajax({  
        url: '<%=basePath%>camera/upload' ,  
        type: 'POST',  
        data: formData,  
        async: false,  
        cache: false,  
        contentType: false,  
        processData: false,  
        success: function (returndata) {  
            console.log(returndata);  
        },  
        error: function (returndata) {  
            console.log("上传失败");  
        }  
   });  
}  






